package com.cj.user.service.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author cheng jun
 * @Description:
 * @Date 2021/5/15 16:26
 */
public class Demo5 {

    private String date;
    private String name;
    private Double temperature;

    public Demo5(String date, String name, Double temperature) {
        this.date = date;
        this.name = name;
        this.temperature = temperature;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getTemperature() {
        return temperature;
    }

    public void setTemperature(double temperature) {
        this.temperature = temperature;
    }

    //初始化数据
    public static List<Demo5> initializationData() {
        List<Demo5> list = new ArrayList<>();
        list.add(new Demo5("2021-05-14 16:42:42", "张三", 36.2));
        list.add(new Demo5("2021-05-12 16:42:42", "李四", 37.0));
        list.add(new Demo5("2021-05-12 16:42:42", "张三", 36.0));
        list.add(new Demo5("2021-05-13 16:42:42", "张三", 36.1));
        list.add(new Demo5("2021-05-13 16:42:42", "赵六", 36.1));
        list.add(new Demo5("2021-05-13 16:42:42", "王五", 36.1));
        list.add(new Demo5("2021-05-14 16:42:42", "王五", 37.2));
        list.add(new Demo5("2021-05-15 16:42:42", "李四", 37.3));
        list.add(new Demo5("2021-05-12 16:42:42", "王五", 36.0));
        list.add(new Demo5("2021-05-13 16:42:42", "李四", 36.1));
        list.add(new Demo5("2021-05-15 16:42:42", "王五", 36.3));
        list.add(new Demo5("2021-05-12 16:42:42", "赵六", 36.0));
        list.add(new Demo5("2021-05-15 16:42:42", "赵六", 36.3));
        list.add(new Demo5("2021-05-14 16:42:42", "赵六", 36.2));
        list.add(new Demo5("2021-05-14 16:42:42", "李四", 36.2));
        return list;
    }

    //时间转换 + 时间排序
    public static List<String> conversionAndOrder(List<Demo5> list){
        //转换排序后的集合
        List<Date> list2 = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        list.forEach(item -> {
            try {
                list2.add(simpleDateFormat.parse(item.getDate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });
        list2.sort((a1, a2) -> {
            return a1.compareTo(a2);
        });

        //去重 + 转字符串
        List<String> list3 = new ArrayList<>();
        list2.stream().distinct().collect(Collectors.toList()).forEach(item -> {
            list3.add(simpleDateFormat.format(item));
        });
        return list3;
    }

    //姓名去重
    public static List<String> deDuplication(List<Demo5> list) {
        //存储人
        List<String> list2 = new ArrayList<>();
        list.forEach(item -> {
            list2.add(item.getName());
        });
        return list2.stream().distinct().collect(Collectors.toList());
    }

    //人和平均温度的映射
    public static List<Map.Entry<String, Double>> nameAndaverageTemperature() {
        //初始化
        List<Demo5> list = initializationData();
        //转换排序后的日期集合
        List<String> dateList = conversionAndOrder(list);

        //去重后的人
        List<String> nameList = deDuplication(list);

        //人和平均温度的映射
        Map<String, Double> nameAndaverageTemperatureMap = new HashMap<>();
        //遍历
        for (String name : nameList) {
            double totalTemperature = 0.00;
            for (String date : dateList) {
                //判断是否有人在某一天没量体温，没有量就填37.5
                boolean flag = false;
                for (Demo5 demo5 : list) {
                    if (name.equals(demo5.getName()) && date.equals(demo5.getDate().substring(0, 10))) {
                        totalTemperature += demo5.getTemperature();
                        //量过体温
                        flag = true;
                    }
                }
                //没有量体温
                if (!flag) {
                    totalTemperature += 37.5;
                }
            }
            nameAndaverageTemperatureMap.put(name, totalTemperature / dateList.size());
        }

        //存储人和平均温度
        List<Map.Entry<String, Double>> nameAndaverageTemperatureList = new ArrayList<>(nameAndaverageTemperatureMap.
                entrySet());
        //平均温度排序
        Collections.sort(nameAndaverageTemperatureList, (o1, o2) ->
                (int) (o2.getValue() - o1.getValue())
        );
        return nameAndaverageTemperatureList;
    }

    //结果
    public static void result(List<Map.Entry<String, Double>> entryList) {
        //初始化
        List<Demo5> list = initializationData();
        //转换排序后的集合
        List<? extends Object> dateList = conversionAndOrder(list);
        System.out.print("      ");
        dateList.forEach(item -> {
            System.out.print(item + "   ");
        });
        System.out.print("   " + "平均温度");
        System.out.println();

        //遍历
        for (int i = entryList.size() - 1; i >= 0; i--) {
            double totalTemperature = 0.00;
            System.out.print(entryList.get(i).getKey() + "   ");
            for (Object date : dateList) {
                //判断是否有人在某一天没量体温，没有量就填37.5
                boolean flag = false;
                for (Demo5 demo5 : list) {
                    if (entryList.get(i).getKey().equals(demo5.getName()) && date.equals(demo5.getDate().toString()
                            .substring(0, 10))) {
                        System.out.print(demo5.getTemperature() + "         ");
                        totalTemperature += demo5.getTemperature();
                        //量过体温
                        flag = true;
                    }
                }
                //没有量体温
                if (!flag) {
                    totalTemperature += 37.5;
                    System.out.print(37.5 + "         ");
                }
            }
            System.out.print("   " + totalTemperature / dateList.size());
            System.out.println();
        }
    }

    public static void main(String[] args) {
        result(nameAndaverageTemperature());
    }
}
